﻿(function ($) {

    $.Validator = function (info) {
        var defOptions = new Array();
        var defValue = {
            id: "",
            type: new Array(
                {
                    name: "null",
                    msg: ""
                },
                {
                    name: "ajax",
                    msg: "",
                    url: ""
                }
            ),
            tipID: ""
        };
        defOptions.push(defValue);
        var gInfo = {
            options: defOptions,
            button: "btn_Post"
        };
        //绑定事件
        var options = info.options;
        if (options.length > 0) {
            var type;
            for (var i = 0; i < options.length; i++) {
                $("#" + options[i].id).blur(function () {
                    Start(options[i]);
                });
            }
        }
    };
    var Start = function (obj) {
        var type = obj.type, id = obj.id, tipID = obj.tipID, result = false;
        //循环判断验证
        for (var i = 0; i < type.length; i++) {
            switch (type[i].name) {
                case "null":
                    result = ValidatorNull($("#" + id).val());
                    break;
                case "number":
                    result = ValidatorIsNaN($("#" + id).val());
                    break;
                case "email":
                    result = ValidatorEmail($("#" + id).val());
                    break;
                case "length":
                    result = ValidatorLength($("#" + id).val(), type[i].minLen, type[i].maxLen);
                    break;
                case "compare":
                    result = ValidatorCompare($("#" + id).val(), $("#" + type[i].compare).val());
                    break;
                case "regular":
                    result = ValidatorRegExp($("#" + id).val(), type[i].regExp);
                    break;
                case "ajax":
                    result = ValidatorAjax($("#" + id), type[i].url);
                    break;
            }
            if (!result) {
                Tips($("#" + id), "<img src='/style/validator/0.gif' alt=''/>" + type[i].msg, tipID, false);
            }
            return result;
        }
        if (result) {
            Tips($("#" + id), "<img src='/style/validator/1.gif' alt=''/>", tipID, true);
        }
        return result;
    };
    //ajax验证(如果验证通过返回1,失败则返回0)
    var ValidatorAjax = function (obj, posturl) {

        var bo = false;
        $.ajax({
            type: 'post',
            url: posturl,
            data: '' + $(obj).attr("id") + '=' + Trim($(obj).val()),
            success: function (result) {
                if (result == "true") {
                    bo = true;
                }
                else {
                    bo = false;
                }
            },
            async: false
        });
        return bo;

    };
    //根据正则验证
    var ValidatorRegExp = function (obj, regExp) {
        var r = regExp;
        if (!r.test(Trim(obj))) {
            return false;
        }
        else {
            return true;
        }
    };
    //验证两个文本值是否相同
    var ValidatorCompare = function (obj, compare) {
        if (Trim(obj) != Trim(compare)) {
            return false;
        }
        else {
            return true;
        }
    };
    //验证字符长度的范围
    var ValidatorLength = function (obj, minLen, maxLen) {
        if (Trim(obj).length < minLen || Trim(obj).length > maxLen) {
            return false;
        }
        else {
            return true;
        }
    };
    //验证邮箱
    var ValidatorEmail = function (obj) {
        var regExp = /^[A-Za-z\d]+([-_\.\+]*[A-Za-z\d]+)*@(([A-Za-z\d]-?){0,62}[A-Za-z\d]\.)+[A-Za-z\d]{2,6}$/;
        if (!regExp.test(Trim(obj))) {
            return false;
        }
        else {
            return true;
        }
    };
    //验证数字
    var ValidatorIsNaN = function (obj) {
        if (isNaN(Trim(obj))) {
            return false;
        }
        else {
            return true;
        }
    };
    //验证是否为空
    var ValidatorNull = function (obj) {
        if (Trim(obj) == "") {
            return false;
        } else {
            return true;
        }
    };
    //输出提示信息
    var Tips = function (obj, tips, tipsid, tf) {
        var id = $(obj).attr("id");
        var color = 'red';
        if (tf) {
            color = 'Green';
        }

        var html = "<span class='validatormsg' style='color:" + color + ";font-size:14px'>" + tips + "</span>";
        if (gValidator.tipsType == 1) {

        } else {
            if (tipsid != "") {
                $("#" + tipsid).empty();
                $("#" + tipsid).append(html);
            } else {
                if ($("#" + id).next() != null) {
                    $("#" + id).next().empty();
                }
                $("#" + id).after(html);
            }
        }
    };
    var Trim = function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
})(jQuery)